package com.csthink.mr.sort.all;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 自定义序列化类步骤
 * 实现Writable接口
 * 默认无参构造
 * 重写write和readFields方法，一定要注意顺序问题
 * toString方法：并不是一定要按照某种参考方式，而是要根据你的功能需求来自定义
 * KV类型都实现实现Writable接口，如果K需要排序，那么K需要实现WritableComparable接口
 *
 * @author <a href="mailto:csthink@icloud.com">Mars</a>
 * @since 2024-04-12 20:09
 */
public class AccessStat implements WritableComparable<AccessStat> {

    private String phone;
    private long up;
    private long down;
    private long sum;

    @Override
    public int compareTo(AccessStat o) {
        return this.sum > o.sum ? 1 : -1;
    }

    // 序列化方法
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(phone);
        out.writeLong(up);
        out.writeLong(down);
        out.writeLong(sum);
    }

    // 反序列化方法：注意顺序问题
    @Override
    public void readFields(DataInput in) throws IOException {
        this.phone = in.readUTF();
        this.up = in.readLong();
        this.down = in.readLong();
        this.sum = in.readLong();
    }

    // 最佳实践：一定要显示的加一个无参构造
    public AccessStat() {
    }

    @Override
    public String toString() {
        return phone + "," + up + "," + down + "," + sum;
    }

    public AccessStat(String phone, long up, long down) {
        this.phone = phone;
        this.up = up;
        this.down = down;
        this.sum = up + down;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public long getUp() {
        return up;
    }

    public void setUp(long up) {
        this.up = up;
    }

    public long getDown() {
        return down;
    }

    public void setDown(long down) {
        this.down = down;
    }

    public long getSum() {
        return sum;
    }

    public void setSum(long sum) {
        this.sum = sum;
    }
}
